package com.xtourms.server;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.xtourms.client.um.UserManagerService;
import com.xtourms.server.db.City;
import com.xtourms.server.db.Province;
import com.xtourms.server.db.Worker;
import com.xtourms.server.db.core.DBOper;
import com.xtourms.shared.field.LocationField;
import com.xtourms.shared.field.WorkerField;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class UserManagerServiceImpl extends RemoteServiceServlet implements
		UserManagerService {

	public String login(String userName, String pwd) throws IllegalArgumentException {
		String ret = "";
		String hql = "from Worker worker where worker.workerUsername=:name and worker.workerPwd=:pwd";
		Session session = DBOper.openSession();
		Query query = session.createQuery(hql);
		query.setParameter("name", userName);
		query.setParameter("pwd", pwd);
		Worker worker = (Worker) query.uniqueResult();
		if(worker != null){
			JSONObject loginInfo = new JSONObject();
			
			try {
				JSONObject wk = new JSONObject();
				wk.put(WorkerField.WORKER_NAME, worker.getWorkerName());
				wk.put(WorkerField.WORKER_ID, worker.getWorkerId());
				wk.put(WorkerField.WORKER_DEP, worker.getWorkerDep());
				loginInfo.put("WORKER", wk);
				loginInfo.put("LOCATION", getLocation());
			} catch (JSONException e) {
				e.printStackTrace();
			}
			ret = loginInfo.toString();
		}
		return ret;
	}

	@SuppressWarnings("unchecked")
	private JSONArray getLocation() throws JSONException{
		JSONArray pvsArray = new JSONArray();
		String hql = "from Province p order by p.provinceId";
		Session session = DBOper.openSession();
		Query query = session.createQuery(hql);
		List<Province> pvsList = query.list();
		for( int index = 0; index < pvsList.size(); index++){
			Province p = pvsList.get(index);
			Integer pId = p.getProvinceId();
			String pName = p.getProvinceName();
			JSONObject jp = new JSONObject();
			
			jp.put(LocationField.PROVINCE_ID, pId);
			jp.put(LocationField.PROVINCE_NAME, pName);
			JSONArray jcs = new JSONArray();
			String chql = "from City c where c.province="+pId+ " order by c.cityId";
			Query cquery = session.createQuery(chql);
			List<City> cityList = cquery.list();
			Iterator<City> csIter = cityList.iterator();
			while(csIter.hasNext()){
				JSONObject jc = new JSONObject();
				City city = csIter.next();
				int cId = city.getCityId();
				String cName = city.getCityName();
				jc.put(LocationField.CITY_ID, cId);
				jc.put(LocationField.CITY_NAME, cName);
				jcs.put(jc);
			}
			jp.put(LocationField.CITYS, jcs);
			pvsArray.put(jp);
		}
		
		return pvsArray;
	}
	
	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
//	private String escapeHtml(String html) {
//		if (html == null) {
//			return null;
//		}
//		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
//				.replaceAll(">", "&gt;");
//	}

	@Override
	public String regist() throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}
}
